package org.jsola.hr.web;

import org.jsola.common.FileKit;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.SocDispatchEmpAddAddDTO;
import org.jsola.hr.dto.SocDispatchEmpAddAndItemUpdateDTO;
import org.jsola.hr.exception.HrException;
import org.jsola.hr.query.SocDispatchEmpAddQuery;
import org.jsola.hr.service.ISocDispatchEmpAddService;
import org.jsola.hr.vo.SocDispatchEmpAddListVO;
import org.jsola.hr.vo.SocDispatchEmpAddVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.File;
import java.util.List;


/**
 * 社保派单员工表_增员
 *
 * @author zhr
 */
@RestController("hrSocDispatchEmpAddController")
@RequestMapping(HrConstants.API_PATH + "/v1/socDispatchEmpAdds")
public class SocDispatchEmpAddController implements ISocDispatchEmpAddControllerAPI {

    @Autowired
    private ISocDispatchEmpAddService socDispatchEmpAddService;

    @Override
    @PostMapping
    public Result<SocDispatchEmpAddVO> save(@Valid @RequestBody SocDispatchEmpAddAddDTO socDispatchEmpAddAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增社保派单员工表_增员权限校验

        return Result.success(
                socDispatchEmpAddService.save(socDispatchEmpAddAddDTO, tokenUser)
        );
    }





    @Override
    @DeleteMapping(value = "/{socDispatchEmpAddId}")
    public Result<Integer> delete(@PathVariable String socDispatchEmpAddId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除社保派单员工表_增员权限校验

        int count = socDispatchEmpAddService.deleteByIds(tokenUser, socDispatchEmpAddId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] socDispatchEmpAddIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除社保派单员工表_增员权限校验

        int count = socDispatchEmpAddService.deleteByIds(tokenUser, socDispatchEmpAddIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{socDispatchEmpAddId}")
    public Result<SocDispatchEmpAddVO> findById(@PathVariable String socDispatchEmpAddId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看社保派单员工表_增员权限校验

        return Result.success(
            socDispatchEmpAddService.selectById(socDispatchEmpAddId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<SocDispatchEmpAddListVO>> listAll(@Valid SocDispatchEmpAddQuery socDispatchEmpAddQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看社保派单员工表_增员权限校验

        return Result.success(
            socDispatchEmpAddService.select(socDispatchEmpAddQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<SocDispatchEmpAddListVO>> page(@Valid SocDispatchEmpAddQuery socDispatchEmpAddQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            socDispatchEmpAddService.selectPage(socDispatchEmpAddQuery, tokenUser.getSiteId())
        );
    }

    /**
     * 临时保存到本地
     *
     * @param file 上传的文件
     * @return 临时文件
     */
    private File getInportExcel(MultipartFile file) {
        String excelFilePath = FileKit.saveFileToLocal(file, null, null, new String[]{"xls", "xlsx"}, null);
        File excelFile = new File(excelFilePath);
        HrException.isTrue(excelFile.exists(), "文件上传失败");
        return excelFile;
    }

    @Override
    @PutMapping("/updateEmpAndItem")
    public Result<Integer> updateEmpAndItem(@Valid @RequestBody SocDispatchEmpAddAndItemUpdateDTO empAndItemUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改社保派单员工表_增员权限校验

        return Result.success(
                socDispatchEmpAddService.updateEmpAndItem(empAndItemUpdateDTO, tokenUser)
        );
    }



}

